package com.project.XXXX.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import sun.misc.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.project.XXXX.controllerInterface.IFinishInfoController;
import com.project.XXXX.inter.tbl_employeeinfoMapper;
import com.project.XXXX.inter.tbl_employerinfoMapper;
import com.project.XXXX.inter.tbl_sysuserMapper;
import com.project.XXXX.model.tbl_employeeinfo;
import com.project.XXXX.model.tbl_employerinfo;
import com.project.XXXX.model.tbl_sysuser;
import com.project.XXXX.tools.ProjectTools;

@Controller

public class FinishInfoControllerImpl implements IFinishInfoController {
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;

	@RequestMapping(value = "/finishEEInfo.do", method = RequestMethod.POST)
	public void FinishEEInfo(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		tbl_employeeinfo tbl_employeeinfo = tbl_employeeinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		tbl_employeeinfo.setMobile(tbl_sysuser.getMobile());
		tbl_employeeinfo.setCreatetime(tbl_sysuser.getCreatetime());
		Date birthday = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
		try {
			birthday = sdf.parse(request.getParameter("birthday"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		tbl_employeeinfo.setBirthday(birthday);
		tbl_employeeinfo.setEmail(request.getParameter("email"));
		tbl_employeeinfo.setGender(request.getParameter("gender"));

		tbl_employeeinfo
				.setInschoolid(request.getParameter("inschoolid") == null ? "11" : request.getParameter("inschoolid"));
		tbl_employeeinfo.setTellus(request.getParameter("tellus"));
		tbl_employeeinfo.setSchool(request.getParameter("school"));
		tbl_employeeinfo.setMajor(request.getParameter("major"));
		tbl_employeeinfo.setModifytime(new Date());
		tbl_employeeinfo.setPwd(tbl_sysuser.getPwd());
		tbl_employeeinfo.setRealname(request.getParameter("realname"));
		tbl_employeeinfo.setTheyear(request.getParameter("theyear"));
		tbl_employeeinfo.setUsername(request.getParameter("username"));
		tbl_employeeinfo.setWechatid(request.getParameter("wechatid"));
		String _bookpttypeids[] = request.getParameterValues("bookpttypeids");
		if (_bookpttypeids != null && _bookpttypeids.length > 0) {
			String _booked = "";
			for (int i = 0; i < _bookpttypeids.length; i++) {
				_booked = _booked + (_bookpttypeids[i] + '&');
			}

			tbl_employeeinfo.setBookpttypeids(_booked);
		}
		tbl_employeeinfoMapper.updateByPrimaryKey(tbl_employeeinfo);
		tbl_sysuser.setEmail(request.getParameter("email"));
		tbl_sysuser.setModifytime(new Date());
		if (request.getParameter("username") != null)
			tbl_sysuser.setNickname(request.getParameter("username"));
		else
			tbl_sysuser.setNickname(request.getParameter("realname"));
		tbl_sysuser.setUsername(request.getParameter("realname"));
		tbl_sysuserMapper.updateByPrimaryKey(tbl_sysuser);
		session.setAttribute("User", tbl_sysuser);
		session.setAttribute("EEinfo", tbl_employeeinfo);
		try {
			response.sendRedirect("EEIndex.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping("/showEEInfo.do")
	public void ShowEEInfo(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		tbl_employeeinfo tbl_employeeinfo = tbl_employeeinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		String json = new Gson().toJson(tbl_employeeinfo);

		ProjectTools.ResponseOut(response, json);

	}

	@RequestMapping(value = "/finishERInfo.do", method = RequestMethod.POST)
	public void FinishERInfo(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		tbl_employerinfo.setMobile(tbl_sysuser.getMobile());
		tbl_employerinfo.setCreatetime(tbl_sysuser.getCreatetime());
		tbl_employerinfo.setComment(request.getParameter("comment"));

		tbl_employerinfo.setEmail(request.getParameter("email"));
		tbl_employerinfo.setDivisionid(request.getParameter("s_county"));
		tbl_employerinfo.setMaibox(tbl_sysuser.getEmail());
		tbl_employerinfo.setScale(request.getParameter("scale"));
		tbl_employerinfo.setTel(request.getParameter("tel"));
		tbl_employerinfo.setModifytime(new Date());
		tbl_employerinfo.setPwd(tbl_sysuser.getPwd());
		tbl_employerinfo.setName(request.getParameter("name"));
		tbl_employerinfo.setAddress(request.getParameter("address"));
		// System.out.println(request.getParameter("s_county"));
		tbl_employerinfo.setWechatid(request.getParameter("wechatid"));
		tbl_employerinfo.setContactor(request.getParameter("contactor"));
		tbl_employerinfoMapper.updateByPrimaryKey(tbl_employerinfo);
		tbl_sysuser.setModifytime(new Date());
		tbl_employerinfo.setModifytime(new Date());

		session.setAttribute("User", tbl_sysuser);
		session.setAttribute("ERinfo", tbl_employerinfo);
		try {
			response.sendRedirect("ERIndex.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping(value = "/showERInfo.do", method = RequestMethod.POST)
	public void ShowERInfo(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");

		tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		String json = new Gson().toJson(tbl_employerinfo);

		ProjectTools.ResponseOut(response, json);

	}

	@RequestMapping(value = "/finishERLogo.do", method = RequestMethod.POST)
	public void finishERLogo(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session,
			@RequestParam(value = "img") String image) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");

	//	tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		tbl_employerinfo tbl_employerinfo =(tbl_employerinfo)session.getAttribute("ERinfo");
		if(tbl_employerinfo==null)
			 tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		File f;
		Map<Object, String> map=new HashMap<Object,String>();
		try {
			String rootPath = getClass().getResource("./").getFile().toString();

			rootPath = rootPath.substring(0, rootPath.indexOf("WEB-INF"));
			File dir = new File(rootPath + "/employer/logo/");
			String name ="defaultlogo";
			String path = FilenameUtils.separatorsToSystem(dir + "/" + name + ".jpg");
			if(StringUtils.equals(tbl_employerinfo.getLogo(), "/employer/logo/defaultlogo.jpg")||StringUtils.isBlank(tbl_employerinfo.getLogo()))
			{
				 name = RandomStringUtils.random(10, true, true)+System.currentTimeMillis()+ ".jpg";
				 path = FilenameUtils.separatorsToSystem(dir + "/" + name  );
				 tbl_employerinfo.setLogo("/employer/logo/" + name);
				 tbl_employerinfoMapper.updateByPrimaryKey(tbl_employerinfo);
			}
			else {				
				path=FilenameUtils.separatorsToSystem(path=rootPath + tbl_employerinfo.getLogo());
			}
			f = new File(path);
			if (f.exists())
				f.delete();
			if (!dir.exists())
				dir.mkdirs();
			if (!f.exists())
				f.createNewFile();
			// image.transferTo(f);

	
		
			FileOutputStream fout1 = null;
			fout1 = new FileOutputStream(f);
			fout1.write(new BASE64Decoder().decodeBuffer(image));
			fout1.close();
			map.put("state", "success");
			map.put("success_description", "logo is update");
			
			session.setAttribute("ERinfo", tbl_employerinfo);
		} catch (IOException e) {
			map.put("state", "error");
			map.put("error_description", "logo is not update");
			e.printStackTrace();
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(map));

	}
	@RequestMapping(value = "/finishEELogo.do", method = RequestMethod.POST)
	public void finishEELogo(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session,
			@RequestParam(value = "img") String image) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");

		tbl_employeeinfo tbl_employeeinfo =(tbl_employeeinfo)session.getAttribute("EEinfo");
		if(tbl_employeeinfo==null)
			tbl_employeeinfo = tbl_employeeinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		File f;
		Map<Object, String> map=new HashMap<Object,String>();
		try {
			String rootPath = getClass().getResource("./").getFile().toString();

			rootPath = rootPath.substring(0, rootPath.indexOf("WEB-INF"));
			File dir = new File(rootPath + "/employer/logo/");
			String name ="default";
			String path = FilenameUtils.separatorsToSystem(dir + "/" + name + ".jpg");
			if(StringUtils.equals(tbl_employeeinfo.getLabelid(), "/employee/logo/default.jpg")||StringUtils.isBlank(tbl_employeeinfo.getLabelid()))
			{
				 name = RandomStringUtils.random(10, true, true)+System.currentTimeMillis()+ ".jpg";
				 path = FilenameUtils.separatorsToSystem(dir + "/" + name  );
				 tbl_employeeinfo.setLabelid ("/employee/logo/" + name);
				 tbl_employeeinfoMapper.updateByPrimaryKey(tbl_employeeinfo);
			}
			else {				
				path=FilenameUtils.separatorsToSystem(path=rootPath + tbl_employeeinfo.getLabelid());
			}
			f = new File(path);
			if (f.exists())
				f.delete();
			if (!dir.exists())
				dir.mkdirs();
			if (!f.exists())
				f.createNewFile();

			FileOutputStream fout1 = null;
			fout1 = new FileOutputStream(f);
			fout1.write(new BASE64Decoder().decodeBuffer(image));
			fout1.close();
			map.put("state", "success");
			map.put("success_description", "logo is update");
			session.setAttribute("EEinfo", tbl_employeeinfo);
		} catch (IOException e) {
			map.put("state", "error");
			map.put("error_description", "logo is not update");
			e.printStackTrace();
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(map));

	}
/*	@RequestMapping(value = "/aaa.do")
	public void aaa(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session,
			@RequestParam(value = "avatar_file") MultipartFile image,@RequestParam(value = "avatar_data") String avatar_data,@RequestParam(value = "avatar_src") String avatar_src) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
System.out.println(avatar_src);
System.out.println(avatar_data);
System.out.println("---------------------------");
System.out.println(image);
		tbl_employeeinfo tbl_employeeinfo = tbl_employeeinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
		File f;
		Map<Object, String> map=new HashMap<Object,String>();
		try {
			String rootPath = getClass().getResource("./").getFile().toString();

			rootPath = rootPath.substring(0, rootPath.indexOf("WEB-INF"));
			File dir = new File(rootPath + "/employee/logo/");
			String name = RandomStringUtils.random(10, true, true);
			String path = FilenameUtils.separatorsToSystem(dir + "/" + name + ".jpg");
			// File dir=new File(request.getContextPath() +"/Employer/logo/");
			if (!StringUtils.isBlank(tbl_employeeinfo.getLabelid() )) {
				f = new File(rootPath + tbl_employeeinfo.getLabelid());
				if (f.exists())
					f.delete();
			}
			f = new File(path);
			tbl_employeeinfo.setLabelid("/employee/logo/" + name + ".jpg");
			if (!dir.exists())
				dir.mkdirs();

			if (!f.exists())
				f.createNewFile();
		image.transferTo(f);

	
			map.put("state", "success");
			map.put("success_description", "logo is update");
			tbl_employeeinfoMapper.updateByPrimaryKey(tbl_employeeinfo);
			session.setAttribute("EEinfo", tbl_employeeinfo);
		} catch (IOException e) {
			map.put("state", "error");
			map.put("error_description", "logo is not update");
			e.printStackTrace();
		}
		ProjectTools.ResponseOut(response, new Gson().toJson(map));

	}*/
}
